package com.cwnu.service.impl;

import com.cwnu.entity.Category;
import com.cwnu.mapper.CategoryMapper;
import com.cwnu.service.CategoryService;
import com.cwnu.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Category findByCategoryName(String categoryName) {
        Category category = categoryMapper.findByCategoryName(categoryName);
        return category;
    }

    @Override
    public Category findByNameAndId(String categoryName, Integer id) {
        return categoryMapper.findByNameAndId(categoryName, id);
    }

    @Override
    public void createCategory(String categoryName) {
        Integer createId = TokenUtil.getUserId();
        categoryMapper.add(categoryName, createId);
    }

    @Override
    public void updateCategory(String oldName, String newName) {
        // 根据用户id和旧类别名获取--> 类别id
        Integer userId = TokenUtil.getUserId();
        Category category = categoryMapper.findByNameAndId(oldName, userId);
        categoryMapper.updateCategory(category.getId(), newName);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteCategory(id);
    }

    @Override
    public List<Category> getCategory() {
        Integer userId = TokenUtil.getUserId();
        return categoryMapper.getCategory(userId);
    }

    @Override
    public List<Category> getAllCategory() {
        return categoryMapper.getAllCategory();
    }
}
